package com.jqb.hibernatebbs.service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jqb.hibernatebbs.pojo.Bbsuser;
import com.jqb.hibernatebbs.util.SessionHandler;

public class BbsUserService {
	
	/**
	 * 验证登录
	 * */
	public Bbsuser checkUser(Bbsuser user) {

		Session session = SessionHandler.getSession();
		Transaction ts = null;

		try {
			ts = session.beginTransaction();
			String hql = "from Bbsuser where name='" + user.getName()
					+ "'and password='" + user.getPassword() + "'";
			Query query = session.createQuery(hql);
			//确定只有一条记录用uniqueResult效率最高
			user = (Bbsuser) query.uniqueResult();
			ts.commit();
		} catch (Exception e) {
			if (ts != null) {
				ts.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return user;
	}

	public static BbsUserService newInstance() {
		return new BbsUserService();
	}
}
